package com.smbms.service.user;

import cn.hutool.core.util.ObjectUtil;
import com.smbms.dao.user.UserDao;
import com.smbms.dao.user.UserDaoImpl;
import com.smbms.pojo.User;
import com.smbms.utils.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 描述
 * @Author 羽落陌殇
 * @Date 2021/7/16 17:05
 * @Version 1.0.0
 */
public class UserServiceImpl implements UserService {
    // 业务层都会调用数据处理层, 所以我们需要引入Dao层
    private UserDao userDao = new UserDaoImpl();

    // 用户登录业务处理
    @Override
    public User login(String userCode, String userPassword) {
        Connection connection = DBUtils.getConnection();
        User loginUser = null;
        if (ObjectUtil.isNotNull(connection)) {
            try {
                loginUser = userDao.getLoginUser(connection, userCode);
                if (loginUser.getUserPassword().equals(userPassword)) {
                    return loginUser;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public int updateUserPassword(int id, String userPassword) {
        int result = 0;
        Connection connection = DBUtils.getConnection();
        if (ObjectUtil.isNotNull(connection)) {
            try {
                result = userDao.updateUserPassword(connection, id, userPassword);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public int getUserCount(String userName, int userRole) {
        int count = 0;
        Connection connection = DBUtils.getConnection();
        if (ObjectUtil.isNotNull(connection)) {
            try {
                count = userDao.getUserCount(connection, userName, userRole);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }

    @Override
    public List<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) {
        Connection connection = DBUtils.getConnection();
        List<User> userList = new ArrayList<>();
        if (ObjectUtil.isNotNull(connection)) {
            try {
                userList = userDao.getUserList(connection, userName, userRole, currentPageNo, pageSize);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return userList;
    }

    @Override
    public boolean userNameIsExists(String userCode) {
        Connection connection = DBUtils.getConnection();
        boolean result = false;
        if (ObjectUtil.isNotNull(connection)) {
            try {
                result = userDao.userNameIsExists(connection, userCode);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public int createUser(User user) {
        Connection connection = DBUtils.getConnection();
        int result = 0;
        if (ObjectUtil.isNotNull(connection)) {
            try {
                result = userDao.createUser(connection, user);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public User queryUserById(int id) {
        Connection connection = DBUtils.getConnection();
        User user = null;
        if (ObjectUtil.isNotNull(connection)) {
            try {
                user = userDao.queryUserById(connection, id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    @Override
    public boolean updateUser(User user) {
        Connection connection = DBUtils.getConnection();
        int result = 0;
        if (ObjectUtil.isNotNull(connection)) {
            try {
                result = userDao.updateUser(connection, user);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (result > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUser(int id) {
        Connection connection = DBUtils.getConnection();
        int result = 0;
        if (ObjectUtil.isNotNull(connection)) {
            try {
                result = userDao.deleteUser(connection, id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (result > 0) {
            return true;
        }
        return false;
    }
}
